//
//  SISSTenantTakeLookCell.m
//  SISSAPP
//
//  Created by 袁应荣 on 2018/1/3.
//  Copyright © 2018年 优房网. All rights reserved.
//

#import "SISSTenantTakeLookCell.h"
#import "SISSTenantTakeLookModel.h"
#import "SISSTenantTakeLookAppointmentModel.h"

@interface SISSTenantTakeLookCell ()

/* labelsView */
@property (weak, nonatomic) UIView *labelsView;
/// Label
@property (weak, nonatomic) UILabel *topLabel;
/// 需求信息Label
@property (weak, nonatomic) UILabel *infoLabel;
@property (strong, nonatomic) NSArray<UILabel *> *markLabels;
/// 看房时间Label
@property (weak, nonatomic) UILabel *timeTitleLabel;
/// 时间Label
@property (weak, nonatomic) UILabel *timeLabel;
/// 拒绝预约
@property (weak, nonatomic) UIButton *refuseButton;
/// 同意预约
@property (weak, nonatomic) UIButton *confirmButton;
/// 底部线条
@property (weak, nonatomic) UIView *lineView;

@end
@implementation SISSTenantTakeLookCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setTenantTakeLookModel:(SISSTenantTakeLookModel *)tenantTakeLookModel {
    _tenantTakeLookModel = tenantTakeLookModel;
    
    self.topLabel.text = [NSString stringWithFormat:@"%@ | %@元",tenantTakeLookModel.lease.neighbourhood,tenantTakeLookModel.lease.monthlyRent];
    self.infoLabel.text = [NSString stringWithFormat:@"%@-%@ | %@㎡ | %@",tenantTakeLookModel.lease.region,tenantTakeLookModel.lease.regionDetail,tenantTakeLookModel.lease.area,tenantTakeLookModel.lease.houseType];
    self.timeLabel.text = [tenantTakeLookModel.takelookDate substringToIndex:10];
    NSArray *houseFeatureArr = tenantTakeLookModel.lease.houseFeature.length == 0 ? @[] : [tenantTakeLookModel.lease.houseFeature componentsSeparatedByString:@"#"];
    for (UILabel *label in self.markLabels) {
        label.hidden = YES;
    }
    
    for (NSInteger i = 0; i < houseFeatureArr.count ; i++) {
        UILabel *label = self.markLabels[i];
        label.hidden = NO;
        label.text = houseFeatureArr[i];
        label.layer.borderWidth = 1;
        label.text = [NSString stringWithFormat:@"%@",houseFeatureArr[i]];
        UIColor *color = [SISSPublicMethods matchTextColorWithHouseFeatureStr:houseFeatureArr[i]];
        label.layer.borderColor = color.CGColor;
        label.textColor = color;
        label.layer.cornerRadius = 2;
        label.layer.masksToBounds = YES;
    }
    
    [self.labelsView mas_updateConstraints:^(MASConstraintMaker *make) {
        if (tenantTakeLookModel.lease.houseFeature.length == 0) {
            if (tenantTakeLookModel.businessType == 2) {
                make.height.mas_equalTo(0);
            } else {
                make.height.mas_equalTo(80);
            }
        } else {
            make.height.mas_equalTo(self.markLabels[houseFeatureArr.count - 1].frame.origin.y + 30);
        }
    }];
}

- (void)setTakeLookAppointmentModel:(SISSTenantTakeLookAppointmentModel *)takeLookAppointmentModel {
    _takeLookAppointmentModel = takeLookAppointmentModel;
    
    self.topLabel.text = [NSString stringWithFormat:@"%@ | %@元",takeLookAppointmentModel.lease.neighbourhood,takeLookAppointmentModel.lease.monthlyRent];
    self.infoLabel.text = [NSString stringWithFormat:@"%@-%@ | %@㎡ | %@",takeLookAppointmentModel.lease.region,takeLookAppointmentModel.lease.regionDetail,takeLookAppointmentModel.lease.area,takeLookAppointmentModel.lease.houseType];
    self.timeLabel.text = [takeLookAppointmentModel.takelookDate substringToIndex:10];
    NSArray *houseFeatureArr = takeLookAppointmentModel.lease.houseFeature.length == 0 ? @[] : [takeLookAppointmentModel.lease.houseFeature componentsSeparatedByString:@"#"];
    for (UILabel *label in self.markLabels) {
        label.hidden = YES;
    }
    
    for (NSInteger i = 0; i < houseFeatureArr.count ; i++) {
        UILabel *label = self.markLabels[i];
        label.hidden = NO;
        label.text = houseFeatureArr[i];
        label.layer.borderWidth = 1;
        label.text = [NSString stringWithFormat:@"%@",houseFeatureArr[i]];
        UIColor *color = [SISSPublicMethods matchTextColorWithHouseFeatureStr:houseFeatureArr[i]];
        label.layer.borderColor = color.CGColor;
        label.textColor = color;
        label.layer.cornerRadius = 2;
        label.layer.masksToBounds = YES;
    }
    
    [self.labelsView mas_updateConstraints:^(MASConstraintMaker *make) {
        if (takeLookAppointmentModel.lease.houseFeature.length == 0) {
            make.height.mas_equalTo(80);
        } else {
            make.height.mas_equalTo(self.markLabels[houseFeatureArr.count - 1].frame.origin.y + 30);
        }
        
    }];
    
    if (takeLookAppointmentModel.state == 1) {
        self.refuseButton.hidden = YES;
        self.confirmButton.hidden = YES;
        [self.confirmButton mas_updateConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.timeTitleLabel.mas_bottom).offset(0);
            make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH - 32, 0));
        }];
    }
}

- (void)setIsOneButton:(BOOL)isOneButton {
    _isOneButton = isOneButton;
    
    self.refuseButton.hidden = isOneButton;
    if (isOneButton) {
        [self.confirmButton setTitle:@"完善看房记录" forState:UIControlStateNormal];
        [self.confirmButton mas_updateConstraints:^(MASConstraintMaker *make) {
            make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH - 32, 32));
        }];
    }
}

// 拒绝预约点击事件
- (void)refuseButtonClickAction:(UIButton *)button {
    
    if ([self.delegate respondsToSelector:@selector(tenantTakeLookCell:clickButtonWithIndex:)]) {
        [self.delegate tenantTakeLookCell:self clickButtonWithIndex:0];
    }
}

// 同意预约点击事件
- (void)confirmButtonClickAction:(UIButton *)button {
    
    if ([self.delegate respondsToSelector:@selector(tenantTakeLookCell:clickButtonWithIndex:)]) {
        [self.delegate tenantTakeLookCell:self clickButtonWithIndex:1];
    }
}

#pragma mark - UI
- (void)initUI {
    [super initUI];
    
    UIView *labelsView = [[UIView alloc] init];
    labelsView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    labelsView.layer.cornerRadius = 4;
    [self.contentView addSubview:labelsView];
    self.labelsView = labelsView;
    
    UILabel *topLabel = [[UILabel alloc] init];
    topLabel.textColor = MAIN_TEXT_COLOR;
    topLabel.font = [UIFont systemFontOfSize:13];
    topLabel.text = @"小区 | 租金";
    [self.labelsView addSubview:topLabel];
    self.topLabel = topLabel;
 
    UILabel *infoLabel = [[UILabel alloc] initWithFrame:CGRectMake(8, 0, SCREEN_WIDTH, 28)];
    infoLabel.textColor = MAIN_TEXT_COLOR;
    infoLabel.font = [UIFont systemFontOfSize:13];
    infoLabel.text = @"区域 | 面积 | 户型";
    [self.labelsView addSubview:infoLabel];
    self.infoLabel = infoLabel;
    
    NSMutableArray<UILabel *> *arrM = [NSMutableArray arrayWithCapacity:6];
    float labelX = 8;
    float labelY = 72;
    CGFloat labelWidth = 66;
    for (NSInteger i = 0; i < 6; i++) {
        if (labelX + labelWidth + 20 > SCREEN_WIDTH) {
            labelX = 8;
            labelY += 30;
        }
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(labelX, labelY, labelWidth, 24)];
        label.textColor = SECONDARY_TEXT_COLOR;
        label.font = [UIFont systemFontOfSize:13];
        label.textAlignment = NSTextAlignmentCenter;
        label.text = @"房源特色";
        label.layer.borderWidth = 1;
        label.layer.cornerRadius = 4;
        label.layer.borderColor = SECONDARY_TEXT_COLOR.CGColor;
        //        label.layer.masksToBounds = YES;
        [arrM addObject:label];
        [self.labelsView addSubview:label];
        
        labelX = CGRectGetMaxX(label.frame) + 10;
        label.hidden = YES;
    }
    self.markLabels = arrM;
    
    UILabel *timeTitleLabel = [[UILabel alloc] init];
    timeTitleLabel.textColor = MAIN_TEXT_COLOR;
    timeTitleLabel.font = [UIFont systemFontOfSize:13];
    timeTitleLabel.text = @"预约时间：";
    [self.contentView addSubview:timeTitleLabel];
    self.timeTitleLabel = timeTitleLabel;
    
    UILabel *timeLabel = [[UILabel alloc] init];
    timeLabel.textColor = MAIN_TEXT_COLOR;
    timeLabel.font = [UIFont systemFontOfSize:13];
    timeLabel.text = @"时间";
    [self.contentView addSubview:timeLabel];
    self.timeLabel = timeLabel;
    
    UIButton *refuseButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [refuseButton setTitle:@"拒绝预约" forState:UIControlStateNormal];
    [refuseButton setTitleColor:SECONDARY_TEXT_COLOR forState:UIControlStateNormal];
    refuseButton.layer.borderWidth = 2;
    refuseButton.layer.borderColor = MAIN_COLOR.CGColor;
    refuseButton.layer.cornerRadius = 8;
    [refuseButton addTarget:self action:@selector(refuseButtonClickAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:refuseButton];
    self.refuseButton = refuseButton;
    
    UIButton *confirmButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [confirmButton setTitle:@"同意预约" forState:UIControlStateNormal];
    [confirmButton setTitleColor:SECONDARY_TEXT_COLOR forState:UIControlStateNormal];
    confirmButton.layer.borderWidth = 2;
    confirmButton.layer.borderColor = MAIN_COLOR.CGColor;
    confirmButton.layer.cornerRadius = 8;
    [confirmButton addTarget:self action:@selector(confirmButtonClickAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:confirmButton];
    self.confirmButton = confirmButton;
    
    UIView *lineView = [[UIView alloc] init];
    lineView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    [self.contentView addSubview:lineView];
    self.lineView = lineView;
}


#pragma mark - 约束
- (void)initConstraint {
    [super initConstraint];
    
    CGFloat margin = 8.0;
    CGSize buttonSize = CGSizeMake((SCREEN_WIDTH - 60) * 0.5, 32);
    WeakSelf
    [self.labelsView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.equalTo(weakSelf.contentView).offset(2 * margin);
        make.right.equalTo(weakSelf.contentView).offset(-2 * margin);
        make.height.mas_equalTo(weakSelf.markLabels[weakSelf.markLabels.count - 1].frame.origin.y + 72);
    }];
    
    [self.topLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.equalTo(weakSelf.labelsView).offset(2 * margin);
    }];
    
    [self.infoLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.topLabel);
        make.top.equalTo(weakSelf.topLabel.mas_bottom).offset(2 * margin);
    }];
    
    [self.timeTitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.labelsView);
        make.top.equalTo(weakSelf.labelsView.mas_bottom).offset(2 * margin);
    }];
    
    [self.timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.timeTitleLabel.mas_right);
        make.centerY.equalTo(weakSelf.timeTitleLabel);
    }];
    
    [self.confirmButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.timeTitleLabel.mas_bottom).offset(2 * margin);
        make.right.equalTo(weakSelf.labelsView);
        make.size.mas_equalTo(buttonSize);
    }];
    
    [self.refuseButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(weakSelf.confirmButton);
        make.left.equalTo(weakSelf.labelsView);
        make.size.mas_equalTo(buttonSize);
    }];
    
    [self.lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.confirmButton.mas_bottom).offset(margin);
        make.left.right.bottom.equalTo(weakSelf.contentView);
        make.height.mas_equalTo(1);
    }];
}


@end
